{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !wget https://huggingface.co/datasets/mesolitica/NER-augmentation/resolve/main/entities-data-v4.json\n",
    "# !wget https://huggingface.co/datasets/mesolitica/NER-augmentation/resolve/main/event-augmentation.json\n",
    "# !wget https://huggingface.co/datasets/mesolitica/NER-augmentation/resolve/main/law-augmentation.json\n",
    "# !wget https://huggingface.co/datasets/mesolitica/NER-augmentation/resolve/main/location-augmentation.json\n",
    "# !wget https://huggingface.co/datasets/mesolitica/NER-augmentation/resolve/main/name-augmentation.json\n",
    "# !wget https://huggingface.co/datasets/mesolitica/NER-augmentation/resolve/main/org-augmentation.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "with open('entities-data-v4.json') as fopen:\n",
    "    data = json.load(fopen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6574"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X, Y = [], []\n",
    "l = 100\n",
    "overlap = 10\n",
    "for i in range(0, len(data['text']), overlap):\n",
    "    X.append(data['text'][i: i + l])\n",
    "    Y.append(data['label'][i: i + l])\n",
    "    \n",
    "len(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(X)):\n",
    "    if len(X[i]) != len(Y[i]):\n",
    "        print(X[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "train_X, test_X, train_Y, test_Y = train_test_split(X, Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['event-augmentation.json',\n",
       " 'org-augmentation.json',\n",
       " 'name-augmentation.json',\n",
       " 'law-augmentation.json',\n",
       " 'location-augmentation.json']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from glob import glob\n",
    "\n",
    "files = glob('*-augmentation.json')\n",
    "files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6930 1944\n",
      "8930 2244\n",
      "10930 2544\n",
      "12028 2830\n",
      "14028 3130\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "for f in files:\n",
    "    with open(f) as fopen:\n",
    "        data = json.load(fopen)\n",
    "    \n",
    "    l = min(len(data['train_X']), 2000)\n",
    "    l = random.sample(range(len(data['train_X'])), l)\n",
    "    x = [data['train_X'][s] for s in l]\n",
    "    y = [data['train_Y'][s] for s in l]\n",
    "    train_X.extend(x)\n",
    "    train_Y.extend(y)\n",
    "    \n",
    "    l = min(len(data['test_X']), 300)\n",
    "    l = random.sample(range(len(data['test_X'])), l)\n",
    "    x = [data['test_X'][s] for s in l]\n",
    "    y = [data['test_Y'][s] for s in l]\n",
    "    test_X.extend(x)\n",
    "    test_Y.extend(y)\n",
    "    \n",
    "    print(len(train_X), len(test_X))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('prepared.json', 'w') as fopen:\n",
    "    json.dump({\n",
    "        'train_X': train_X,\n",
    "        'train_Y': train_Y,\n",
    "        'test_X': test_X,\n",
    "        'test_Y': test_Y\n",
    "    }, fopen)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
